/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ruang;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rploo_ciayoo.kontrol.KoneksiDatabase;

public class RuangKontrol {

    private Connection conn;

    public RuangKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static RuangKontrol getKoneksi() throws SQLException {
        KoneksiDatabase konek = new KoneksiDatabase();
        RuangKontrol kon = new RuangKontrol(konek.getDBConnection());
        return kon;
    }

    public void tambahRuang(Ruang data) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "insert into ruang "
                    + "(id_ruang)"
                    + "values (?)";
            statement = conn.prepareStatement(sql);
            statement.setString(1, data.getId_ruang());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

    public List<Ruang> lihatDataRuang() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);

            statement = conn.prepareStatement("Select id_ruang "
                    + "from ruang "
                    + "order by id_ruang");

            result = statement.executeQuery();
            List<Ruang> data = new ArrayList<Ruang>();
            while (result.next()) {
                Ruang rg = new Ruang();
                rg.setId_ruang(result.getString(1));

                data.add(rg);
            }
            conn.commit();
            return data;
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

    
    public void hapusPrepared(Ruang dataRuang) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "delete from ruang where id_ruang = ?";
            statement = conn.prepareStatement(sql);
            statement.setString(1, dataRuang.getId_ruang());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException ex) {
                throw ex;
            }
        }
    }
}
